<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件驱动和数据驱动</title>
</head>

<body>
    <div>
        <div>商品数量<input id="goodsnum" /></div>
        <div>商品价格<input id="goodsprice" /></div>
        <div>小计<span id="total"></span></div>
    </div>

</body>
<script>
    let goodsnumInput = document.getElementById("goodsnum");
    let goodspriceInput = document.getElementById("goodsprice");
    let totalSpan = document.getElementById("total");

    let  goodsinfo = {};//商品对象

    Object.defineProperty(goodsinfo,"goodsnum",{
        set(n){//设置
           _goodsnum= n;
           complier();
        },
        get(){//获取
            return _goodsnum;
        }
    })

    Object.defineProperty(goodsinfo,"goodsprice",{
        set(n){//设置
           _goodsprice= n;
           complier();
        },
        get(){//获取
            return _goodsprice;
        }
    })

    function complier(){

        totalSpan.innerHTML  = goodsinfo.goodsnum * goodsinfo.goodsprice;
    }


    goodsnumInput.oninput = function () {
        goodsinfo.goodsnum = this.value;
        
    }

    goodspriceInput.oninput = function () {
        goodsinfo.goodsprice =this.value;
        
    }







</script>

</html>